#include "appconfig.h"

AppConfig::AppConfig()
{
	m_null_device = createDevice(video::EDT_NULL);

	m_driver_options[TEXT("Software")]=video::EDT_SOFTWARE;
	m_driver_options[TEXT("OpenGL")] = video::EDT_OPENGL;
	m_driver_options[TEXT("Direct3D9")] = video::EDT_DIRECT3D9;
 /*
	m_resolution_options[TEXT("640x480")] = core::dimension2du(640, 480);
	m_resolution_options[TEXT("800x600")] = core::dimension2du(800, 600);
	m_resolution_options[TEXT("1024x768")] = core::dimension2du(1024, 768);
	m_resolution_options[TEXT("1152x864")] = core::dimension2du(640, 480);
	m_resolution_options[TEXT("1280x1024")] = core::dimension2du(640, 480);
	m_resolution_options[TEXT("1360x768")] = core::dimension2du(800, 600);
	m_resolution_options[TEXT("1366x768")] = core::dimension2du(1024, 768);
	m_resolution_options[TEXT("640x480")] = core::dimension2du(640, 480);
	m_resolution_options[TEXT("800x600")] = core::dimension2du(800, 600);  
	m_resolution_options[TEXT("1024x768")] = core::dimension2du(1024, 768);
	m_resolution_options[TEXT("640x480")] = core::dimension2du(640, 480);
	m_resolution_options[TEXT("800x600")] = core::dimension2du(800, 600);
	m_resolution_options[TEXT("2560x1600")] = core::dimension2du(1024, 768);
 */
	m_core_config[TEXT("driver")]=TEXT("OpenGL");
	m_core_config[TEXT("resolution")]=TEXT("1024x768");
	m_core_config[TEXT("fullscreen")]=TEXT("0");
}

AppConfig::~AppConfig()
{
	if (m_null_device != NULL)
	{
		m_null_device->closeDevice(); 
	}
}

st_config& AppConfig::get_config()
{
	return m_cfg;
}

bool AppConfig::read()
{
	return read(m_cfg.cfg_path);
}

bool AppConfig::read(io::path path)
{ 
	if (!m_null_device)
		return false;

	io::path fullpath = m_cfg.app_path + path; 
	//io::IXMLReader* xml = m_null_device->getFileSystem()->createXMLReader(fullpath); 
	autoref<io::IXMLReader> xml = m_null_device->getFileSystem()->createXMLReader(fullpath);

	if (!xml)
		return false;

	const core::stringw config_tag(L"config");  
	const core::stringw graphic_tag(L"video");  
	const core::stringw node_tag(L"setting");

	core::stringw current_node;

	while (xml->read())
	{  
		switch (xml->getNodeType())
		{ 
		case io::EXN_ELEMENT:
		{    
			if (current_node.empty() && config_tag.equals_ignore_case(xml->getNodeName()))
			{
				current_node = config_tag;
			} 
			else if (current_node.equals_ignore_case(config_tag) &&
 				graphic_tag.equals_ignore_case(xml->getNodeName()))
			{ 
				current_node = graphic_tag;
			}
			else if (current_node.equals_ignore_case(graphic_tag) &&
				node_tag.equals_ignore_case(xml->getNodeName()))
			{
				core::stringw key = xml->getAttributeValueSafe(TEXT("name")); 
				if (!key.empty())
				{
					m_core_config[key] = xml->getAttributeValueSafe(TEXT("value"));
				}
			}
		}
		break;   

		case irr::io::EXN_ELEMENT_END: 
			current_node = TEXT("");
			break;
		}
	}

	return true;
}


bool AppConfig::save()
{
	return save(m_cfg.cfg_path);
}

bool AppConfig::save(io::path path)
{ 
	if (!m_null_device)
		return false;

	io::path fullpath = m_cfg.app_path + path;

	//create xml writer
	autoref<irr::io::IXMLWriter> xwriter = m_null_device->getFileSystem()->createXMLWriter(fullpath);

	if (!xwriter)
		return false;

	//write out the obligatory xml header. Each xml-file needs to have exactly one of those.
	xwriter->writeXMLHeader();

	//start element	mygame, you replace the label "mygame" with anything you want
	xwriter->writeElement(L"config");
	xwriter->writeLineBreak();					 
 
	xwriter->writeElement(L"video");
	xwriter->writeLineBreak();					 

	std::map<core::stringw, core::stringw>::iterator i = m_core_config.begin();
	for ( i = m_core_config.begin(); i!=m_core_config.end(); ++i)
	{
		//write element as <setting name="key" value="x" />
		//the second parameter indicates this is an empty element with no children, just attributes
		xwriter->writeElement(L"setting", true, L"name", i->first.c_str(), L"value", i->second.c_str());
		xwriter->writeLineBreak();
	}  
 
	xwriter->writeClosingTag(L"video");
	xwriter->writeLineBreak(); 
	xwriter->writeClosingTag(L"config");
 
	return true;
} 

bool AppConfig::make_config()
{ 
	if (!m_null_device)
		return false; 

	XmlTree *tree = new XmlTree(); 

	//root node
	st_xml_node *root_node = new st_xml_node(L"config"); 

	//catogory nodes
	st_xml_node *video_node = new st_xml_node(L"video"); 
	st_xml_node *path_node = new st_xml_node(L"path"); 
	st_xml_node *res_node = new st_xml_node(L"property"); 
	st_xml_node *tank_node = new st_xml_node(L"tank");
	st_xml_node *alien_node = new st_xml_node(L"alien"); 

	//video
	video_node->child.push_back(new st_xml_node(L"driver", L"D3D9")); 
	tree->build_tree(root_node); 
	tree->add_childnode_by_name(L"config", new st_xml_node(L"video"));
	tree->add_childnode_by_name(L"config", new st_xml_node(L"path"));
	tree->add_childnode_by_name(L"config", new st_xml_node(L"property")); 
	tree->add_childnode_by_name(L"video", new st_xml_node(L"fullscreen",L"1"));
	
	save(tree); 
}

bool AppConfig::save(XmlTree *tree)
{ 
	io::path fullpath = m_cfg.app_path + L"\\test.xml";

	autoref<irr::io::IXMLWriter> xwriter = m_null_device->getFileSystem()->createXMLWriter(fullpath);

	if (!xwriter)
		return false; 

	tree->save_tree(xwriter);

	return true;
}
